const mongoose = require("mongoose");
//第三层
//删除
module.exports.deletOne3 = async function ({ _id }) {
  const data = await mongoose.model("goodsModel").deleteOne({ _id });
  return data;
};
//搜索
module.exports.search3 = async ({
  nowPage,
  pageSize,
  searchType,
  searchValue,
}) => {
  // var _filter={
  //     $or: [  // 多字段同时匹配
  //       {name: {$regex: searchValue,$options: '$i'}},
  //       {num: {$regex: searchValue, $options: '$i'}}, //  $options: '$i' 忽略大小写
  //       {en: {$regex: searchValue, $options: '$i'}}
  //     ]
  //   }
  const allGoods = await mongoose.model("goodsModel").find();
  const total = await mongoose
    .model("goodsModel")
    .find({ [searchType]: { $regex: searchValue, $options: "$i" } })
    .populate("typesId")
    .countDocuments();
  const goods = await mongoose
    .model("goodsModel")
    .find({ [searchType]: { $regex: searchValue, $options: "$i" }})
    // {[searchType]:{$regex:searchValue,$options:'$i'}}
    .populate("typesId")
    .populate("shopId")
    .limit(pageSize - 0) //显示多少条
    .skip((nowPage - 1) * pageSize); //跳过页数
  const upGoods = await mongoose
    .model("goodsModel")
    .find({ [searchType]: { $regex: searchValue, $options: "$i" },putaway: true })
    // {[searchType]:{$regex:searchValue,$options:'$i'}}
    .populate("typesId")
    .populate("shopId")
    .limit(pageSize - 0) //显示多少条
    .skip((nowPage - 1) * pageSize); //跳过页数
  const notupGoods = await mongoose
    .model("goodsModel")
    .find({ [searchType]: { $regex: searchValue, $options: "$i" },putaway: false })
    // {[searchType]:{$regex:searchValue,$options:'$i'}}
    .populate("typesId")
    .populate("shopId")
    .limit(pageSize - 0) //显示多少条
    .skip((nowPage - 1) * pageSize); //跳过页数

  console.log(2132132131, goods, total);

  return {
    goods,
    allGoods,
    total,
    upGoods,
    notupGoods
    

  };
};

// module.exports.typessearch = async ({ nowPage, pageSize, searchType, searchValue }) => {

//     const typesdata= await mongoose.model('typesModel').find({name:searchType});

// }
//渲染
module.exports.getAll3 = async ({ nowPage, pageSize }) => {
  const allGoods = await mongoose.model("goodsModel").find();
  const total = await mongoose
    .model("goodsModel")
    .find()
    .populate("typesId")
    .countDocuments();
  const goods = await mongoose
    .model("goodsModel")
    .find()
    .populate("typesId")
    .populate("shopId")
    .limit(pageSize - 0) //显示多少条
    .skip((nowPage - 1) * pageSize); //跳过页数
  console.log(allGoods, goods, "渲染");
  const notupGoods = await mongoose
    .model("goodsModel")
    .find({ putaway: false })
    .populate("typesId")
    .populate("shopId")
    .limit(pageSize - 0) //显示多少条
    .skip((nowPage - 1) * pageSize); //跳过页数;
  const upGoods = await mongoose
    .model("goodsModel")
    .find({ putaway: true })
    .populate("typesId")
    .populate("shopId")
    .limit(pageSize - 0) //显示多少条
    .skip((nowPage - 1) * pageSize); //跳过页数;
  return {
    goods,
    allGoods,
    total,
    notupGoods,
    upGoods,
  };
};
//新增
module.exports.addOne3 = async (goods) => {
  const data = await mongoose.model("goodsModel").create(goods);
  return data;
};
//修改  1.获取
module.exports.getOne3 = async (_Id) => {
  return await mongoose.model("goodsModel").find(_Id);
};
//修改 2.确认修改
module.exports.updateOne3 = async ({
  _id,
  name,
  price,
  num,
  details,
  listImage,
  detailImage,
  comments,
  typesId,
  shopId,
}) => {
  return await mongoose.model("goodsModel").updateOne(
    { _id },
    {
      name,
      price,
      num,
      details,
      listImage,
      detailImage,
      comments,
      typesId,
      shopId,
    }
  );
};
module.exports.updateMany3 = async ({ _id, shopId, putaway }) => {
  console.log(_id, shopId, putaway);
  const data = await mongoose
    .model("goodsModel")
    .updateMany({ _id }, { $set: { shopId, putaway } });
  console.log(data);
  return data;
};
module.exports.getTypes3 = async (typesId) => {
  return await mongoose.model("goodsModel").find(typesId);
};
